package com.example.myapplication

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.myapplication.camera1.TakeFacePhotoActivity
import com.media.photocamera.utils.CameraConstans
import com.media.photocamera.utils.CameraFileUtils
import com.example.myapplication.x.demo.CameraXDemoActivity01
import com.media.photocamera.CameraStart
import com.media.photocamera.imagelist.CameraImageBean
import java.io.File

class MainActivity : AppCompatActivity() {
    private val STORAGE_REQUEST_CODE = 0
    var testresult: ImageView? = null
    var info: TextView? = null
    var button: Button? = null
    var xphotocount: Button? = null
    var sizelimit: EditText? = null
    var countlimit: EditText? = null
    var isFornt: CheckBox? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        testresult = this.findViewById<ImageView>(R.id.testresult)
        button = this.findViewById<Button>(R.id.button)
        info = this.findViewById<Button>(R.id.info)
        sizelimit = this.findViewById<EditText>(R.id.sizelimit)
        countlimit = this.findViewById<EditText>(R.id.countlimit)
        xphotocount = this.findViewById<Button>(R.id.xphotocount)
        isFornt = this.findViewById<CheckBox>(R.id.isFornt)
        button?.setOnClickListener {
            val intent = Intent(this@MainActivity, TakeFacePhotoActivity::class.java)
            intent.putExtra(CameraConstans.FACEPHOTO_scale, 0.75)
            intent.putExtra( CameraConstans.FACEPHOTO_widthScale,0.8);
            //                                    intent.putExtra( Constans.FACEPHOTO_widthScale,(double) 0.99);
            intent.putExtra(CameraConstans.FACEPHOTO_light, 1)
            val isfornt=if (isFornt?.isChecked!!) 1 else 0
            intent.putExtra(CameraConstans.FACEPHOTO_isFront,isfornt )
            intent.putExtra(CameraConstans.FACEPHOTO_isSaveToAlbum, 1)
            intent.putExtra(CameraConstans.FACEPHOTO_limit, sizelimit?.text.toString().toInt())
            this@MainActivity.startActivityForResult(intent, CameraConstans.START_FACEPHOTO_CODE)

//            startActivity(Intent(this@MainActivity2, TakeFacePhotoActivity::class.java))
        }
        this.findViewById<Button>(R.id.x).setOnClickListener {
            startActivity(Intent(this@MainActivity, CameraXDemoActivity01::class.java))
        }
        this.findViewById<Button>(R.id.x2)?.setOnClickListener {
//            val intent = Intent(this@MainActivity, com.media.photocamera.activity.CameraXFacePhotoActivity::class.java)
//            intent.putExtra(CameraConstans.FACEPHOTO_scale, 0.75)
//            intent.putExtra( CameraConstans.FACEPHOTO_widthScale,0.8);
//            //                                    intent.putExtra( Constans.FACEPHOTO_widthScale,(double) 0.99);
//            intent.putExtra(CameraConstans.FACEPHOTO_light, 1)
//            val isfornt=if (isFornt?.isChecked!!) 1 else 0
//            intent.putExtra(CameraConstans.FACEPHOTO_isFront,isfornt )
//            intent.putExtra(CameraConstans.FACEPHOTO_isSaveToAlbum, 1)
//            intent.putExtra(CameraConstans.FACEPHOTO_limit, sizelimit?.text.toString().toInt())
//            this@MainActivity.startActivityForResult(intent, CameraConstans.START_FACEPHOTO_CODE)
            CameraStart.CameraStartBuilder()
                    .setFaceCut(true)
                    .setFaceScale(0.75)
                    .setFaceWidthScale(0.8)
                    .setMaxCount( 1)
                    .setLight(true)
                    .setIsFrontFirst(isFornt?.isChecked!!)
                    .setIsSaveToAlbum(true)
                    .setSizeLimit(sizelimit?.text.toString().toInt())
                    .build().startActivityForResult(this, CameraConstans.START_FACEPHOTO_CODE)

//            startActivity(Intent(this@MainActivity2, TakeFacePhotoActivity::class.java))
        }
        this.findViewById<Button>(R.id.xphoto)?.setOnClickListener {
//            val intent = Intent(this@MainActivity, com.media.photocamera.activity.CameraXActivity::class.java)
//            intent.putExtra(CameraConstans.FACEPHOTO_scale, 0.75)
//            intent.putExtra( CameraConstans.FACEPHOTO_widthScale,0.8);
//            // intent.putExtra( Constans.FACEPHOTO_widthScale,(double) 0.99);
//            intent.putExtra(CameraConstans.FACEPHOTO_light, 1)
//            val isfornt=if (isFornt?.isChecked!!) 1 else 0
//            intent.putExtra(CameraConstans.FACEPHOTO_isFront,isfornt )
//            intent.putExtra(CameraConstans.FACEPHOTO_isSaveToAlbum, 1)
//            intent.putExtra(CameraConstans.FACEPHOTO_limit, sizelimit?.text.toString().toInt())
//            this@MainActivity.startActivityForResult(intent, CameraConstans.START_PHOTOLIST_CODE)
            CameraStart.CameraStartBuilder()
                    .setMaxCount( 1)
                    .setLight(true)
                    .setIsFrontFirst(isFornt?.isChecked!!)
                    .setIsSaveToAlbum(true)
                    .setSizeLimit(sizelimit?.text.toString().toInt())
                    .build().startActivityForResult(this, CameraConstans.START_PHOTOLIST_CODE)

//            startActivity(Intent(this@MainActivity2, TakeFacePhotoActivity::class.java))
        }
        this.findViewById<Button>(R.id.xphotocount)?.setOnClickListener {
//            val intent = Intent(this@MainActivity, com.media.photocamera.activity.CameraXActivity::class.java)
//            intent.putExtra(CameraConstans.FACEPHOTO_scale, 0.75)
//            intent.putExtra( CameraConstans.FACEPHOTO_widthScale,0.8);
//            // intent.putExtra( Constans.FACEPHOTO_widthScale,(double) 0.99);
//            intent.putExtra(CameraConstans.FACEPHOTO_light, 1)
//            val isfornt=if (isFornt?.isChecked!!) 1 else 0
//            intent.putExtra(CameraConstans.FACEPHOTO_isFront,isfornt )
//            intent.putExtra(CameraConstans.FACEPHOTO_count,  countlimit?.text.toString().toInt())
//            intent.putExtra(CameraConstans.FACEPHOTO_isSaveToAlbum, 1)
//            intent.putExtra(CameraConstans.FACEPHOTO_limit, sizelimit?.text.toString().toInt())
//            this@MainActivity.startActivityForResult(intent, CameraConstans.START_PHOTOLIST_CODE)

            CameraStart.CameraStartBuilder()
                    .setMaxCount( countlimit?.text.toString().toInt())
                    .setLight(true)
                    .setIsFrontFirst(isFornt?.isChecked!!)
                    .setIsSaveToAlbum(true)
                    .setSizeLimit(sizelimit?.text.toString().toInt())
                    .build().startActivityForResult(this, CameraConstans.START_PHOTOLIST_CODE)

//            startActivity(Intent(this@MainActivity2, TakeFacePhotoActivity::class.java))
        }
        ActivityCompat.requestPermissions(
            this,
            arrayOf(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ),
            STORAGE_REQUEST_CODE
        )
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!== PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                STORAGE_REQUEST_CODE
            )
        }


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!== PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 0)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == CameraConstans.START_FACEPHOTO_CODE && resultCode == RESULT_OK) {
            try {
                val stringExtra = data!!.getStringExtra(CameraConstans.CALL_BACK_FACEPHOTO_CODE)
                val bitmap = BitmapFactory.decodeFile(stringExtra)
                val readableFileSize: String = CameraFileUtils.getReadableFileSize(File(stringExtra).length())
                testresult?.setImageBitmap(bitmap)
                testresult?.visibility= View.VISIBLE
                info?.setText("高度：${bitmap.height} 宽度：${bitmap.width} 大小：${readableFileSize} path:${stringExtra}")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        } else if (requestCode == CameraConstans.START_PHOTOLIST_CODE && resultCode == RESULT_OK) {
            try {
                val imageList:ArrayList<CameraImageBean> = data!!.getSerializableExtra(CameraConstans.CALL_BACK_PHOTOLIST_CODE) as ArrayList<CameraImageBean>
                var sb=StringBuffer();
                for( imagebean in imageList){
                    val bitmap = BitmapFactory.decodeFile(imagebean.path)
                    val readableFileSize: String = CameraFileUtils.getReadableFileSize(File(imagebean.path).length())
                    testresult?.setImageBitmap(bitmap)
                    sb.append("高度：${bitmap.height} 宽度：${bitmap.width} 大小：${readableFileSize} path:${imagebean.path} \n");
                 }
                info?.setText(sb)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}